selenium中关于 stale element reference:element is not attached to the page document的解决办法

您所在的位置:网站首页 stale cookies翻译 selenium中关于 stale element reference:element is not attached to the page document的解决办法

selenium中关于 stale element reference:element is not attached to the page document的解决办法

2023-07-22 09:16| 来源: 网络整理| 查看: 265

     有时候在执行程序时会遇到如下报错内容:stale element reference:element is not attached to the page document。

    原始程序:

modules=browser.find_element_by_xpath('//*[@id="sidebar"]/div[2]').find_elements_by_tag_name('li') time.sleep(2) for i in range(len(modules)): time.sleep(3) module=modules[i].text

 

就多加了一行,相当于重新获取一次xpath地址:

modules=browser.find_element_by_xpath('//*[@id="sidebar"]/div[2]').find_elements_by_tag_name('li') time.sleep(2) for i in range(len(modules)): time.sleep(3) modules = browser.find_element_by_xpath('//*[@id="sidebar"]/div[2]').find_elements_by_tag_name('li') module=modules[i].text

主要思路就在于,看报错信息是哪一行,就在那一行前面重新定位一下元素即可,如果有循环存在,适当sleep一下,保证每个循环都能完美执行。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3